package com.example.fushuang.balabala.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.fushuang.balabala.R;
import com.example.fushuang.balabala.model.RecommendEntry;
import com.squareup.picasso.Picasso;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by admin on 2016/10/13.
 */

public class RecommendRecycleAdapter extends RecyclerView.Adapter {

    public static final int TYPE_RECOMMEND = 1;
    public static final int TYPE_LIVE = 2;
    public static final int TYPE_BANGUMI = 3;
    public static final int TYPE_REGION = 4;
    public static final int TYPE_WEB_LINK = 5;
    public static final int TYPE_ACTIVITY = 6;
    public static final int TYPE_IMG=7;

    private Context mContext;

    private final List<RecommendEntry.ResultBean> mList;
    private static final String TAG = "RecommendRecycleAdapter";

    public RecommendRecycleAdapter(Context context, List<RecommendEntry.ResultBean> resultBeanList) {
        mContext = context;
        mList = resultBeanList;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder ret = null;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View item;
        switch (viewType) {
            case TYPE_RECOMMEND:
                item = inflater.inflate(R.layout.recommend_recycle_item_recommend, parent, false);
                ret = new RecommendViewHolder(item);
                break;
            case TYPE_LIVE:
                item = inflater.inflate(R.layout.recommend_recycle_item_live, parent, false);
                ret = new LiveViewHolder(item);
                break;
            case TYPE_BANGUMI:
                item = inflater.inflate(R.layout.recommend_recycle_item_bangumi, parent, false);
                ret = new Bangumi2ViewHolder(item);
                break;
            case TYPE_REGION:
                item = inflater.inflate(R.layout.recommend_recycle_item_region, parent, false);
                ret = new RegionViewHolder(item);
                break;
            case TYPE_WEB_LINK:
                item = inflater.inflate(R.layout.recommend_recycle_item_weblink, parent, false);
                ret = new WebLinkViewHolder(item);
                break;
            case TYPE_ACTIVITY:
                item = inflater.inflate(R.layout.recommend_recycle_item_recommend, parent, false);
                ret = new ActivityViewHolder(item);
                break;
            case TYPE_IMG:
                item = inflater.inflate(R.layout.recommend_recycle_item_img, parent, false);
                ret=new ImgViewHolder(item);
                break;
            default:

                break;

        }

        return ret;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        RecommendEntry.ResultBean itemCard = mList.get(position);
        if (holder instanceof SimpleViewHolder) {
            ((SimpleViewHolder) holder).bindView(itemCard);
        }else if(holder instanceof ImgViewHolder){
            ((ImgViewHolder) holder).bindView(itemCard);
        }else if (holder instanceof WebLinkViewHolder){
            ((WebLinkViewHolder) holder).bindView(itemCard);
        }

    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            int size = mList.size();
            return size;
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        int ret = 0;
        RecommendEntry.ResultBean result = mList.get(position);
        String type = result.getType();
        if (type==null){
            ret=7;
        }else {
            switch (type) {
                case "recommend":
                    ret = 1;
                    break;
                case "live":
                    ret = 2;
                    break;
                case "bangumi_2":
                    ret = 3;
                    break;
                case "region":
                    ret = 4;
                    break;
                case "weblink":
                    ret = 5;
                    break;
                case "activity":
                    ret = 6;
                    break;

            }
        }

        return ret;
    }


    private static class SimpleViewHolder extends RecyclerView.ViewHolder {

        private SparseArrayCompat<View> mSparseArray; //这是一个类似于map的东西

        public SimpleViewHolder(View itemView) {
            super(itemView);
            mSparseArray = new SparseArrayCompat<>();
        }

        /**
         * 此方法要求子类重写,将数据添加到空间上
         */
        void bindView(RecommendEntry.ResultBean bean) {

        }


        /**
         * id是int数值是没有规律的,但是id字符串的名字有规律,可以通过for循环进行拼接,话不啰嗦直接反射R.id类,通过名字进行查找
         *
         * @param name 子控件的名称
         * @return
         */
        View getChildView(String name) {

            View ret = null;
            Class<R.id> idClass = R.id.class;
            try {
                Field field = idClass.getField(name);
                field.setAccessible(true);
                int id = field.getInt(idClass); //本来这个地方应该是传一个对象实例的,但是R.id这个类是个静态final 的,所以传入类名就行了
                if (id != 0) {
                    ret = getChildView(id);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return ret;
        }

        View getChildView(int id) {

            View ret = null;
            ret = mSparseArray.get(id);
            if (ret == null) {
                //itemView是RecyclerView.ViewHolder中的属性,已经实现对视图的复用
                ret = itemView.findViewById(id);
                if (ret != null) {
                    mSparseArray.put(id, ret);
                }
            }
            return ret;
        }

    }

    private static class RecommendViewHolder extends SimpleViewHolder implements View.OnClickListener {

        public RecommendViewHolder(View itemView) {
            super(itemView);
            for (int i = 0; i < 4; i++) {
                View card = getChildView("recommend_item_recommend_cardView_" + i);

                card.setOnClickListener(this);
            }
        }

        //重写simpleViewHolder中的bindView方法
        @Override
        void bindView(RecommendEntry.ResultBean bean) {
            super.bindView(bean);
            String title = bean.getHead().getTitle();
            ((ImageView) itemView.findViewById(R.id.recommend_item_recommend_title_icon)).setImageResource(R.drawable.ic_header_hot);
            TextView textView = (TextView) itemView.findViewById(R.id.recommend_item_recommend_title);
            textView.setText(title);
            //填充四个cardView
            List<RecommendEntry.ResultBean.BodyBean> body = bean.getBody();
            if (body.size() >= 4) {
                for (int i = 0; i < 4; i++) {
                    RecommendEntry.ResultBean.BodyBean cardItem = body.get(i);
                    //给每个cardItem设置tag,跳转的时候传递tag就行,不需要知道点击的是哪一个空间
                    CardView cardView = (CardView) getChildView("recommend_item_recommend_cardView_" + i);
                    cardView.setTag(cardItem);

                    ImageView imageView = (ImageView) getChildView("recommend_item_recommend_cardView_img_" + i);
                    TextView cardTitle = (TextView) getChildView("recommend_item_recommend_cardView_title_" + i);
                    TextView count = (TextView) getChildView("recommend_item_recommend_cardView_count_" + i);
                    TextView danmaku = (TextView) getChildView("recommend_item_recommend_cardView_danmaku_" + i);

                    cardTitle.setText(cardItem.getTitle());
                    count.setText(cardItem.getPlay());
                    danmaku.setText(cardItem.getDanmaku());

                    Picasso.with(imageView.getContext())
                            .load(cardItem.getCover())
                            .config(Bitmap.Config.RGB_565)
                            .resize(320, 200)
                            .into(imageView);
                }
            }

        }

        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            if (tag != null) {
                if (tag instanceof RecommendEntry.ResultBean.BodyBean) {
                    // EventBus 传递对象给 当前Fragment, 由Fragment 来跳转到 详情
                    RecommendEntry.ResultBean.BodyBean bodyItem = (RecommendEntry.ResultBean.BodyBean) tag;
                    EventBus.getDefault().post(bodyItem);
                }
            }
        }
    }


    private static class LiveViewHolder extends SimpleViewHolder implements View.OnClickListener {

        public LiveViewHolder(View itemView) {
            super(itemView);
            for (int i = 0; i < 4; i++) {
                View card = getChildView("recommend_item_recommend_cardView_" + i);

                card.setOnClickListener(this);
            }
        }
        @Override
        void bindView(RecommendEntry.ResultBean bean) {
            super.bindView(bean);
            String title = bean.getHead().getTitle();
            ((ImageView) itemView.findViewById(R.id.recommend_item_recommend_title_icon)).setImageResource(R.drawable.ic_head_live);
            TextView textView = (TextView) itemView.findViewById(R.id.recommend_item_recommend_title);
            textView.setText(title);
            //填充四个cardView
            List<RecommendEntry.ResultBean.BodyBean> body = bean.getBody();
            if (body.size() >= 4) {
                for (int i = 0; i < 4; i++) {
                    RecommendEntry.ResultBean.BodyBean cardItem = body.get(i);
                    //给每个cardItem设置tag,跳转的时候传递tag就行,不需要知道点击的是哪一个空间
                    CardView cardView = (CardView) getChildView("recommend_item_recommend_cardView_" + i);
                    cardView.setTag(cardItem);

                    ImageView imageView = (ImageView) getChildView("recommend_item_recommend_cardView_img_" + i);
                    TextView cardTitle = (TextView) getChildView("recommend_item_recommend_cardView_title_" + i);
                    TextView count = (TextView) getChildView("recommend_item_recommend_cardView_count_" + i);
                    TextView danmaku = (TextView) getChildView("recommend_item_recommend_cardView_danmaku_" + i);

                    cardTitle.setText(cardItem.getTitle());
                    count.setText(cardItem.getPlay());
                    danmaku.setText(cardItem.getDanmaku());

                    Picasso.with(imageView.getContext())
                            .load(cardItem.getCover())
                            .config(Bitmap.Config.RGB_565)
                            .resize(320, 200)
                            .into(imageView);
                }
            }

        }

        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            if (tag != null) {
                if (tag instanceof RecommendEntry.ResultBean.BodyBean) {
                    // EventBus 传递对象给 当前Fragment, 由Fragment 来跳转到 详情
                    RecommendEntry.ResultBean.BodyBean bodyItem = (RecommendEntry.ResultBean.BodyBean) tag;
                    EventBus.getDefault().post(bodyItem);
                }
            }
        }
    }

    private static class Bangumi2ViewHolder extends SimpleViewHolder implements View.OnClickListener {

        public Bangumi2ViewHolder(View itemView) {
            super(itemView);
            for (int i = 0; i < 4; i++) {
                View card = getChildView("recommend_item_recommend_cardView_" + i);

                card.setOnClickListener(this);
            }
        }
        @Override
        void bindView(RecommendEntry.ResultBean bean) {
            super.bindView(bean);
            String title = bean.getHead().getTitle();
            ((ImageView) itemView.findViewById(R.id.recommend_item_recommend_title_icon)).setImageResource(R.drawable.ic_header_topic);
            TextView textView = (TextView) itemView.findViewById(R.id.recommend_item_recommend_title);
            textView.setText(title);
            //填充四个cardView
            List<RecommendEntry.ResultBean.BodyBean> body = bean.getBody();
            if (body.size() >= 4) {
                for (int i = 0; i < 4; i++) {
                    RecommendEntry.ResultBean.BodyBean cardItem = body.get(i);
                    CardView cardView = (CardView) getChildView("recommend_item_recommend_cardView_" + i);
                    cardView.setTag(cardItem);
                    ImageView imageView = (ImageView) getChildView("recommend_item_recommend_cardView_img_" + i);
                    TextView cardTitle = (TextView) getChildView("recommend_item_recommend_cardView_title_" + i);
                    TextView count = (TextView) getChildView("recommend_item_recommend_cardView_count_" + i);
                    TextView danmaku = (TextView) getChildView("recommend_item_recommend_cardView_danmaku_" + i);

                    cardTitle.setText(cardItem.getTitle());
                    count.setText(cardItem.getPlay());
                    danmaku.setText(cardItem.getDanmaku());

                    Picasso.with(imageView.getContext())
                            .load(cardItem.getCover())
                            .config(Bitmap.Config.RGB_565)
                            .resize(320, 200)
                            .into(imageView);
                }
            }

        }

        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            if (tag != null) {
                if (tag instanceof RecommendEntry.ResultBean.BodyBean) {
                    // EventBus 传递对象给 当前Fragment, 由Fragment 来跳转到 详情
                    RecommendEntry.ResultBean.BodyBean bodyItem = (RecommendEntry.ResultBean.BodyBean) tag;
                    EventBus.getDefault().post(bodyItem);
                }
            }
        }
    }

    private static class RegionViewHolder extends SimpleViewHolder implements View.OnClickListener {

        public RegionViewHolder(View itemView) {
            super(itemView);
            for (int i = 0; i < 4; i++) {
                View card = getChildView("recommend_item_recommend_cardView_" + i);

                card.setOnClickListener(this);
            }
        }
        @Override
        void bindView(RecommendEntry.ResultBean bean) {
            super.bindView(bean);
            String title = bean.getHead().getTitle();
            ((ImageView) itemView.findViewById(R.id.recommend_item_recommend_title_icon)).setImageResource(R.drawable.ic_header_ding);
            TextView textView = (TextView) itemView.findViewById(R.id.recommend_item_recommend_title);
            textView.setText(title);
            //填充四个cardView
            List<RecommendEntry.ResultBean.BodyBean> body = bean.getBody();
            if (body.size() >= 4) {
                for (int i = 0; i < 4; i++) {
                    RecommendEntry.ResultBean.BodyBean cardItem = body.get(i);
                    CardView cardView = (CardView) getChildView("recommend_item_recommend_cardView_" + i);
                    cardView.setTag(cardItem);

                    ImageView imageView = (ImageView) getChildView("recommend_item_recommend_cardView_img_" + i);
                    TextView cardTitle = (TextView) getChildView("recommend_item_recommend_cardView_title_" + i);
                    TextView count = (TextView) getChildView("recommend_item_recommend_cardView_count_" + i);
                    TextView danmaku = (TextView) getChildView("recommend_item_recommend_cardView_danmaku_" + i);

                    cardTitle.setText(cardItem.getTitle());
                    count.setText(cardItem.getPlay());
                    danmaku.setText(cardItem.getDanmaku());

                    Picasso.with(imageView.getContext())
                            .load(cardItem.getCover())
                            .config(Bitmap.Config.RGB_565)
                            .resize(320, 200)
                            .into(imageView);
                }
            }

        }

        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            if (tag != null) {
                if (tag instanceof RecommendEntry.ResultBean.BodyBean) {
                    // EventBus 传递对象给 当前Fragment, 由Fragment 来跳转到 详情
                    RecommendEntry.ResultBean.BodyBean bodyItem = (RecommendEntry.ResultBean.BodyBean) tag;
                    EventBus.getDefault().post(bodyItem);
                }
            }
        }
    }

    private static class WebLinkViewHolder extends SimpleViewHolder implements View.OnClickListener {

        public WebLinkViewHolder(View itemView) {
            super(itemView);
        }
        @Override
        void bindView(RecommendEntry.ResultBean bean) {
            super.bindView(bean);

            //填充四个cardView
            List<RecommendEntry.ResultBean.BodyBean> body = bean.getBody();
            if (body.size() >0) {
                RecommendEntry.ResultBean.BodyBean bodyBean = body.get(0);
                String cover = bodyBean.getCover();
                ImageView imageView = (ImageView) itemView.findViewById(R.id.recommend_item_weblink_img);
                imageView.setTag(bodyBean);
                imageView.setOnClickListener(this);
                Picasso.with(imageView.getContext()).load(cover)
                        .config(Bitmap.Config.RGB_565)
                        .into(imageView);

            }
        }

        @Override
        public void onClick(View v) {
            Object tag = v.getTag();
            if (tag != null) {
                if (tag instanceof RecommendEntry.ResultBean.BodyBean) {
                    RecommendEntry.ResultBean.BodyBean bodyItem = (RecommendEntry.ResultBean.BodyBean) tag;
                    EventBus.getDefault().post(bodyItem);
                }
            }
        }

    }

    private static class ActivityViewHolder extends SimpleViewHolder {

        public ActivityViewHolder(View itemView) {
            super(itemView);
        }
        @Override
        void bindView(RecommendEntry.ResultBean bean) {
            super.bindView(bean);
            String title = bean.getHead().getTitle();
            ((ImageView) itemView.findViewById(R.id.recommend_item_recommend_title_icon)).setImageResource(R.drawable.ic_header_new);
            TextView textView = (TextView) itemView.findViewById(R.id.recommend_item_recommend_title);
            textView.setText(title);
            //填充四个cardView
            List<RecommendEntry.ResultBean.BodyBean> body = bean.getBody();
            if (body.size() >= 4) {
                for (int i = 0; i < 4; i++) {
                    RecommendEntry.ResultBean.BodyBean cardItem = body.get(i);
                    ImageView imageView = (ImageView) getChildView("recommend_item_recommend_cardView_img_" + i);
                    TextView cardTitle = (TextView) getChildView("recommend_item_recommend_cardView_title_" + i);
                    TextView count = (TextView) getChildView("recommend_item_recommend_cardView_count_" + i);
                    TextView danmaku = (TextView) getChildView("recommend_item_recommend_cardView_danmaku_" + i);

                    cardTitle.setText(cardItem.getTitle());
                    count.setText(cardItem.getPlay());
                    danmaku.setText(cardItem.getDanmaku());

                    Picasso.with(imageView.getContext())
                            .load(cardItem.getCover())
                            .config(Bitmap.Config.RGB_565)
                            .resize(320, 200)
                            .into(imageView);
                }
            }

        }
    }

    private static class ImgViewHolder extends RecyclerView.ViewHolder {
        public ImgViewHolder(View itemView) {
            super(itemView);
        }
        void bindView(RecommendEntry.ResultBean bean) {
            ImageView imageView = (ImageView) itemView.findViewById(R.id.recommend_item_img);
            Picasso.with(imageView.getContext())
                    .load(bean.getBody().get(0).getCover())
                    .config(Bitmap.Config.RGB_565)
                    .resize(320, 200)
                    .into(imageView);
        }

    }

}
